package cn.dxh.stagefinal.service.impl;

import cn.dxh.stagefinal.bean.Employee;
import cn.dxh.stagefinal.dao.EmployeeDao;
import cn.dxh.stagefinal.service.EmployeeService;
import cn.dxh.stagefinal.utils.MD5Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * IntelliJ IDEA -> stagefinal
 * 2019/12/10 10:57
 *
 * @author zmyao
 * 别看，我是渣渣。
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeDao employeeDao;

    @Override
    public PageInfo<Employee> findEmployees(Integer pageIndex, Integer pageSize, String eName, Integer dId,Integer rLevel) {
        PageHelper.startPage(pageIndex, pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("eName", eName);
        map.put("dId", dId);
        map.put("rLevel", rLevel);
        List<Employee> employees = employeeDao.findEmployees(map);
        return new PageInfo<>(employees);
    }

    @Transactional
    @Override
    public int insertEmployee(Employee employee) {
        Employee e = employeeDao.findEmployeeByLoginName(employee.getLoginName());
        if(e == null){
            Integer dId = employee.getdId();
            Integer rId = employee.getrId();
            if (dId == 0 || rId == 0) {
                return -1;
            }
            employee.seteState(1);
            String salt = MD5Util.createSalt();
            String pwd = MD5Util.createPassword(employee.getePassword(), salt);
            employee.setePassword(pwd);
            employee.setSalt(salt);
            return employeeDao.insertEmployee(employee);
        }
        return -2;
    }

    @Transactional
    @Override
    public int updateemployeepwd(Integer eId, String newPassword, String rePassword) {
        if (rePassword == null || rePassword == null || rePassword == "" || newPassword == "") {
            return -2;
        }
        if (!rePassword.equals(newPassword)) {
            return -1;
        }
        String salt = MD5Util.createSalt();
        String pwd = MD5Util.createPassword(newPassword, salt);
        Map<String, Object> map = new HashMap<>();
        map.put("eId", eId);
        map.put("newPassword", pwd);
        map.put("newSalt", salt);
        return employeeDao.updateemployeepwd(map);
    }

    @Transactional
    @Override
    public int updateemployee(Employee employee) {
        return employeeDao.updateemployee(employee);
    }

    @Override
    public Employee findEmployeeByeId(Integer eId) {
        return employeeDao.findEmployeeByeId(eId);
    }

    @Transactional
    @Override
    public int deleteEmployeeByEid(Integer eId, Integer eState) {
        Map<String, Object> map = new HashMap<>();
        map.put("eId", eId);
        map.put("eState", eState);
        return employeeDao.deleteEmpDidByEid(map);
    }

    @Override
    public int updateEmployeeEstateByEids(int[] eIds,Integer eState ) {
        Map  map = new HashMap();
        map.put("eIds",eIds);
        map.put("eState",eState);
        return  employeeDao.updateEmployeeEstateByEids(map);
    }

    @Override
    public List<Employee> findEmployeesByEids(int[] eIds) {
        return employeeDao.findEmployeesByEids(eIds);
    }

    @Override
    public Employee findEmployeeByLoginName(String loginName) {
      return  employeeDao.findEmployeeByLoginName(loginName);
    }
}
